
// 读取或者写入用户信息
export const Userinfo = (data) => {
    const UserinfoKey = "USERINFO_STORAGE";
    if (data) {
        localStorage.setItem(UserinfoKey, JSON.stringify(data));
    } else {
        var loca = localStorage.getItem(UserinfoKey);
        var ret = {};
        try {
            ret = JSON.parse(loca);
        } catch (error) {
            console.log(error)
            ret = {};
        }
        return ret || {};
    }
}



// 历史搜索
export const HistorySearch = (keyword) => {
    const storagekey = "HistorySearch_STORAGE";
    const maxlen = 20;

    var loca = localStorage.getItem(storagekey);
    var list = [];
    try {
        list = JSON.parse(loca);
    } catch (error) {
        list = [];
    }
    if (!(list instanceof Array)) {
        list = [];
    }


    if (keyword) {
        list.push(keyword);

        // 去重，排序，最新的在最前面
        var map = {};
        for (let i = list.length - 1; i >= 0; i--) {
            map[list[i]] = 1;
        }
        var newlist = [];
        for (const k in map) {
            newlist.push(k);
        }
        
        var newlist2 = newlist.slice(-maxlen);
        localStorage.setItem(storagekey, JSON.stringify(newlist2));
    } else if (keyword === false) {
        localStorage.setItem(storagekey, JSON.stringify([]));
    } else {
        return list.reverse();
    }
}
